﻿/*
 * DemoErrorStackLibrary: A demonstration program for the ErrorStackLibrary
 * Copyright (C) 2009 by Harley Pebley
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the 
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using ErrorStackLibrary;

namespace DemoErrorStackLibaray
{
    public partial class DemoForm
    {
        private readonly ObservableCollection<KeyValuePair<string, string>> Items =
            new ObservableCollection<KeyValuePair<string, string>>();

        public DemoForm()
        {
            InitializeComponent();
            ItemsView.ItemsSource = Items;
        }

        private void KeyValueChanged(object sender, TextChangedEventArgs e)
        {
            AddKeyValue.IsEnabled = !string.IsNullOrEmpty(NewKey.Text) && !string.IsNullOrEmpty(NewValue.Text);
        }

        private void StackKeyEdit_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckSubmitIsEnabled();
        }

        private void AddKeyValue_Click(object sender, RoutedEventArgs e)
        {
            Items.Add(new KeyValuePair<string, string>(NewKey.Text, NewValue.Text));
            NewKey.Text = "";
            NewValue.Text = "";
            CheckSubmitIsEnabled();
        }

        private void CheckSubmitIsEnabled()
        {
            Submit.IsEnabled = !string.IsNullOrEmpty(StackKeyEdit.Text) && Items.Count > 0;
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            var items = new Dictionary<string, string>();
            foreach(var item in Items)
                items.Add(item.Key, item.Value);

            var errorStack = new ErrorStack(StackKeyEdit.Text);
            errorStack.Log(items);
            Items.Clear();
            CheckSubmitIsEnabled();
        }
    }
}
